package com.nccsjz.back.cardbuy.service;

import java.sql.SQLException;

import com.nccsjz.back.cardbuy.dao.CardBuyDAO;
import com.nccsjz.base.BaseService;
import com.nccsjz.pojo.CardBuy;
import com.nccsjz.utils.Page;

/**
 * CardBuyService类用于处理与集体购卡相关的业务操作
 * 
 * @author Administrator
 * 
 */
public class CardBuyService extends BaseService {

	/**
	 * saveCardBuy方法用于保存购卡信息
	 * 
	 * @throws SQLException
	 * 
	 */
	public void saveCardBuy(CardBuy cardbuy) throws SQLException {
		try {
			openCon();
			new CardBuyDAO(con).insertCardBuy(cardbuy);
		} finally {
			closeCon();
		}
	}
	
	/**
	 * removeCardBuy方法根据制定编号移除订购信息
	 * 
	 * @param id
	 * @throws SQLException 
	 */
	public void removeCardBuy(Long id) throws SQLException{
		try{
			openCon();
			new CardBuyDAO(con).deleteCardBuyById(id);
		}finally{
			closeCon();
		}
	}
	
	/**
	 * removeCardBuyBatch方法批量移除订购信息
	 * 
	 * @param ids
	 * @throws Exception 
	 */
	public void removeCardBuyBatch(Long[] ids) throws Exception{
		try{
			openCon();
			CardBuyDAO dao = new CardBuyDAO(con);
			con.setAutoCommit(false);
			for(Long id:ids){
				dao.deleteCardBuyById(id);
			}
			con.commit();
		}catch(Exception e){
			con.rollback();
			e.printStackTrace();
			throw e;
		}finally{
			con.setAutoCommit(true);
			closeCon();
		}
	}

	/**
	 * findCardBuy获取集体购卡信息
	 * 
	 * @param page
	 * @throws SQLException
	 */
	public void findCardBuy(CardBuy cardbuy, Page page) throws SQLException {
		try {
			openCon();
			String state = String.valueOf(cardbuy.getState()); 
			CardBuyDAO dao = new CardBuyDAO(con);
			//查询数据
			dao.getCardBuysByState(state, page);
			page.setRecordCount(dao.getRecordCountByState(state));
		} finally {
			closeCon();
		}
	}
	
	/**
	 * findCardById方法用于根据编号查找订购信息
	 * 
	 * @param id
	 * @throws SQLException 
	 */
	public CardBuy findCardById(Long id) throws SQLException{
		CardBuy cardBuy = null;
		try{
			openCon();
			cardBuy = new CardBuyDAO(con).getCardBuyById(id);
		}finally{
			closeCon();
		}
		return cardBuy;
	}
	
	/**
	 * updateCardBuy方法用于更新订购信息
	 * 
	 * @param cardbuy
	 * @throws SQLException
	 */
	public void updateCardBuy(CardBuy cardbuy) throws SQLException{
		try{
			openCon();
			new CardBuyDAO(con).updateCardBuy(cardbuy);
		}finally{
			closeCon();
		}
	}
}
